<?php

namespace app\admin\controller\curriculum;

use app\admin\{
    controller\Controllers,
    service\curriculum\Curriculum as CurriculumService,
    model\curriculum\Curriculum as CurriculumModels,
    service\curriculum\Teacher
};
use think\Db;

class Curriculum extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( CurriculumService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\curriculum\Curriculum.index');
        $list = $service->indexPaginate($get);
        $config = config('field.curriculum');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'config' => $config
        ]);
        return $this->fetch('curriculum/curriculum/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create( Teacher $teacherService )
    {
        $config = config('field.curriculum');
        $this->assign([
            'config' => $config,
            'teacher' => $teacherService->getList()
        ]);
        return $this->fetch('curriculum/curriculum/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save( CurriculumService $service )
    {
        $post = $this->request->only(['name','cover','start_cycle','end_cycle','type','origin_price','price','detail','tid','category'], 'post');
        $this->validate($post, 'app\admin\validate\curriculum\Curriculum.Create');
        Db::startTrans();
        $curriculumid = $service->create($post);
        if ( false === $curriculumid || false === $service->relatedCreate($curriculumid, $post['tid']) ) {
            Db::rollback();
            return failed('新增失败');
        }
        Db::commit();
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  CurriculumModels $model
     * @return \think\Response
     */
    public function edit( CurriculumModels $model, Teacher $teacherService )
    {
        $config = config('field.curriculum');
        $this->assign([
            'data' => $model,
            'config' => $config,
            'teacher' => $teacherService->getList()
        ]);
        return $this->fetch('curriculum/curriculum/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  CurriculumModels  $model
     * @param  CurriculumService  $service
     * @return \think\Response
     */
    public function update(CurriculumModels $model, CurriculumService $service)
    {
        $put = $this->request->only(['name','cover','start_cycle','end_cycle','type','origin_price','price','detail','tid','category'], 'put');
        $this->validate($put, 'app\admin\validate\curriculum\Curriculum.Update');
        Db::startTrans();
        $update = $service->save($model, $put);
        if ( false === $update || false === $service->relatedDelete($model->id) || false === $service->relatedCreate($model->id, $put['tid']) ) {
            Db::rollback();
            return failed('编辑失败');
        }
        Db::commit();
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, CurriculumService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\curriculum\Curriculum.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @param CurriculumService $service
     * @return \think\Response
     */
    public function destory(CurriculumService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\curriculum\Curriculum.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
